package cn.yl.api.server.util;


import com.alibaba.nacos.client.config.utils.MD5;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author hyl
 */

public class PasswordUtil {

    public static Map<String ,String > getMD5Password(String password) {
        MD5 instance = MD5.getInstance();
        HashMap<String, String> map = new HashMap<>();
        String randomString = getRandomString(4);
        map.put("password",instance.getMD5String(randomString + password));
        map.put("salt",randomString);
        return map;
    }

    public static String getMD5Password(String password, String salt) {
        MD5 instance = MD5.getInstance();
        return instance.getMD5String(salt + password);
    }

    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
}
